package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Indent;
import com.woniuxy.service.indent.IndentService;
import com.woniuxy.service.table.TableDataService;
import com.woniuxy.util.ResultVo;
import com.woniuxy.vo.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 马踏东京赏樱花
 */
@RestController
public class TableDataController {

    @Autowired
    private TableDataService tableDataService;
    @Autowired
    private IndentService indentService;

    @PostMapping("/table/find")
    public ResultVo findCounts() {
        //获取订单总量
        int i = tableDataService.bedsCount();
        //获取总床数
        int bedsSum = tableDataService.findBedsSum();
        //创建TableData对象
        TableData tableData = new TableData();
        //设置订单总量
        tableData.setBedsCount(i);
        //设置总床数
        tableData.setBedsSum(bedsSum);
        //获取所有订单
        List<Indent> list = indentService.findAll();
        //设置初始金额
        BigDecimal money = BigDecimal.valueOf(0);
        //遍历订单，计算总金额
        for (Indent indent : list) {
            money = money.add(indent.getIndentMoney());
        }
        //设置总金额
        tableData.setMoneySum(money);
        //返回结果
        return tableData != null ? ResultVo.success("ok", tableData) : ResultVo.failure("no");
    }

    @PostMapping("/table/findAll")
    public ResultVo findAll() {
        List<Indent> list = tableDataService.findAll();
        return list != null ? ResultVo.success("ok", list) : ResultVo.failure();
    }

    @PostMapping("/table/findAllPage")
    public ResultVo findAllPage(@RequestBody Map<String, String> map) {
        String strPage = map.remove("page");
        String strSize = map.remove("size");
        List<Indent> list = tableDataService.findPage(Integer.valueOf(strPage), Integer.valueOf(strSize));
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return pageInfo != null ? ResultVo.success("ok", pageInfo) : ResultVo.failure();
    }


}
